//
//  FundController.m
//  LeaveLight
//
//  Created by 张 on 2019/3/5.
//  Copyright © 2019年 xsl. All rights reserved.
//

#import "FundController.h"
#import <SPPageMenu/SPPageMenu.h>
#import "FundListController.h"

@interface FundController ()<SPPageMenuDelegate>

@property (strong,nonatomic) UIScrollView *scrollView;
@property (strong,nonatomic) SPPageMenu *pageMenu;
@property (strong,nonatomic) NSMutableArray *childVC;
@property (strong,nonatomic) NSMutableArray *menuList;
@property (strong,nonatomic) UILabel *timeLab;
@property (strong,nonatomic) QMUIButton *weather;
@property (strong,nonatomic) NSMutableArray *classArray;

@end

@implementation FundController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviViewHidden:NO];
    [self hiddenBackBtn];
    self.naviTitle.text = @"发现";
    self.view.backgroundColor = UIColorMakeWithHex(@"#F5F5F5");
    
    [self.view addSubview:self.pageMenu];
    [self.view addSubview:self.scrollView];
    
    [self.naviView addSubview:self.timeLab];
    [self.naviView addSubview:self.weather];
    [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.naviView).offset(StatusBarHeightConstant);
        make.left.equalTo(self.naviView).offset(16);
        make.bottom.equalTo(self.naviView).offset(0);
    }];
    
    [_weather mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.naviView).offset(StatusBarHeightConstant);
        make.right.equalTo(self.naviView).offset(-16);
        make.bottom.equalTo(self.naviView).offset(0);
    }];
    
    [self getFundTypeListSuccess:^(NSMutableArray * _Nonnull typeArr) {
        self.classArray = typeArr;
        [self.menuList removeAllObjects];
        for (NSDictionary *info in self.classArray) {
            [self.menuList addObject:info[@"name"]];
        }
        [self.pageMenu setItems:self.menuList selectedItemIndex:0];
        self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width * self.menuList.count, 0);
        [self childVC];
    } failed:^(NSString * _Nonnull error) {
    }];
    weakify(self);
    [[SystemManager shareManager] getLocalWeatherwithBlock:^(NSString *weatherStr, NSString *weatherImg) {
        strongify(self);
        [self.weather setTitle:weatherStr forState:UIControlStateNormal];
        [self.weather setImage:[UIImage imageNamed:weatherImg] forState:UIControlStateNormal];
    }];
}

- (UILabel *)timeLab{
    if (!_timeLab) {
        _timeLab = [[UILabel alloc] init];
        _timeLab.font = [UIFont systemFontOfSize:14];
        _timeLab.textColor = UIColorMakeWithHex(@"#202020");
        NSDate *date = [NSDate date];
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy.MM.dd";
        NSString *string = [fmt stringFromDate:date];
        _timeLab.text = string;
    }
    return _timeLab;
}

- (QMUIButton *)weather{
    if (!_weather) {
        _weather = [[QMUIButton alloc] init];
        [_weather setImage:[UIImage imageNamed:@"雨"] forState:UIControlStateNormal];
        [_weather setTitle:@"雨" forState:UIControlStateNormal];
        _weather.imagePosition = QMUIButtonImagePositionRight;
        _weather.imageView.contentMode = UIViewContentModeScaleAspectFit;
        _weather.titleLabel.font = [UIFont systemFontOfSize:14];
        [_weather setTitleColor:UIColorMakeWithHex(@"#202020") forState:UIControlStateNormal];
        _weather.spacingBetweenImageAndTitle = 5;
    }
    return _weather;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NavigationContentTopConstant + 44, SCREEN_WIDTH, SCREEN_HEIGHT - TabBarHeight - NavigationContentTopConstant - 44)];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
        _scrollView.backgroundColor = [UIColor whiteColor];
        if (@available(iOS 11.0, *)) {
            _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _scrollView;
}

- (SPPageMenu *)pageMenu{
    if (!_pageMenu) {
        _pageMenu = [[SPPageMenu alloc] initWithFrame:CGRectMake(0, NavigationContentTopConstant + 1, SCREEN_WIDTH, 44) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
        _pageMenu.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _pageMenu.selectedItemTitleFont = UIFontMediumMake(16);
        _pageMenu.unSelectedItemTitleFont = UIFontMediumMake(16);
        _pageMenu.selectedItemTitleColor = UIColorMakeWithHex(@"#FFAF04");
        _pageMenu.unSelectedItemTitleColor = UIColorMakeWithHex(@"#606060");
        _pageMenu.backgroundColor = [UIColor whiteColor];
        _pageMenu.permutationWay = SPPageMenuPermutationWayScrollAdaptContent;
        _pageMenu.tracker.backgroundColor = UIColorMakeWithHex(@"#FFAF04");
        _pageMenu.dividingLineHeight = 0;
        _pageMenu.bridgeScrollView = self.scrollView;
        _pageMenu.delegate = self;
    }
    return _pageMenu;
}

- (NSMutableArray *)childVC{
    if (!_childVC) {
        _childVC = [[NSMutableArray alloc] init];
        CGFloat width = self.scrollView.bounds.size.width;
        CGFloat height = self.scrollView.bounds.size.height;
        for (int i = 0; i < self.menuList.count; i++) {
            FundListController *childVC = [[FundListController alloc] initWithNibName:@"FundListController" bundle:nil];
            childVC.classId = [self.classArray[i][@"id"] integerValue];
            childVC.view.frame = CGRectMake(width * i, 0, width, height);
            [self addChildViewController:childVC];
            [self.scrollView addSubview:childVC.view];
            [_childVC addObject:childVC];
        }
    }
    return _childVC;
}

- (NSMutableArray *)menuList{
    if (!_menuList) {
        _menuList = [[NSMutableArray alloc] init];
    }
    return _menuList;
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
    [UIView animateWithDuration:0.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.scrollView.bounds.size.width * toIndex, 0);
    }];
    FundListController *tmp = self.childVC[toIndex];
    [tmp creatUIAndLoadData];
}



@end
